package com.fyzk.activity.user.userinfo;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.user.BindBankActivity;
import com.fyzk.activity.user.BindAlipayActivity;
import com.fyzk.activity.user.ChangeNickname;
import com.fyzk.bean.BankListResponse;
import com.fyzk.bean.UserInfoResponse;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.dialog.SureDialog;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.TextStyleUtil;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import http.Common.CommandUtil;
import http.DataUtil.text.TextUtil;
import http.StringResultBean;

/**
 * 个人信息界面
 */
@ContentView(R.layout.ac_personalinformation)
public class PersonalInformationActivityNew extends BaseActivity{

    @ViewInject(R.id.personalinfo_topview)
    private TopTitleView topView;
    @ViewInject(R.id.personalinfo_username)
    private TextView userName_tv;
    @ViewInject(R.id.personalinfo_nickname)
    private TextView nickname_tv;
    @ViewInject(R.id.personalinfo_realname)
    private TextView realName_tv;   // 实名认证
    @ViewInject(R.id.personalinfo_phone)
    private TextView phone_tv;

    private BankListResponse bankInfo;
    private BankListResponse alipayInfo;

    @ViewInject(R.id.personalinfo_bank_ll)
    private LinearLayout bank_ll;
    @ViewInject(R.id.personalinfo_alipay_ll)
    private LinearLayout alipay_ll;

    @ViewInject(R.id.personalinfo_bank_info)
    private TextView bankInfo_tv;
    @ViewInject(R.id.personalinfo_alipay_info)
    private TextView alipayInfo_tv;

    @ViewInject(R.id.personalinfo_bank_delete)
    private TextView bankDelete_tv;
    @ViewInject(R.id.personalinfo_alipay_delete)
    private TextView alipayDelete_tv;
    private int deleteType;

    @ViewInject(R.id.personalinfo_bind_bank)
    private TextView bindBank_tv;
    @ViewInject(R.id.personalinfo_bind_alipay)
    private TextView bindAlipay_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

    }

    private void initView() {
        topView.setOnTitleClickListener(this);

        nickname_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(),
                        ChangeNickname.class);
                startActivity(intent);
            }
        });

        realName_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), AuthenticationActivity.class);
                intent.putExtra("userinfo", userInfoResponse);
                startActivity(intent);
            }
        });

        bankDelete_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteType = 0;
                showSureDialog();
            }
        });

        alipayDelete_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteType = 1;
                showSureDialog();
            }
        });

        bindBank_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userInfoResponse == null) {
                    return;
                }
                Intent intent = new Intent(getContext(), BindBankActivity.class);
                intent.putExtra(IntentParamUtils.USERNAME, MySharedPreferences.getInstance().getUserName());
                intent.putExtra(IntentParamUtils.REALNAME, userInfoResponse.getRealName());
                startActivity(intent);
            }
        });

        bindAlipay_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userInfoResponse == null) {
                    return;
                }
                Intent intent = new Intent(getContext(), BindAlipayActivity.class);
                intent.putExtra(IntentParamUtils.USERNAME, MySharedPreferences.getInstance().getUserName());
                intent.putExtra(IntentParamUtils.REALNAME, userInfoResponse.getRealName());
                startActivity(intent);
            }
        });
    }

    //控件赋值
    private void kongJianFuZhi() {
        userName_tv.setText(MySharedPreferences.getInstance().getUserName());

        if (TextUtil.isEmpty(MySharedPreferences.getInstance().getAlias())) {
            nickname_tv.setText("未设置");
            nickname_tv.setClickable(true);
            nickname_tv.setTextColor(Color.parseColor("#CCCCCC"));
//            nickname_tv.setCompoundDrawables(null,null,getResources().getDrawable(R.drawable.rigth_shijiao),null);
        } else {
            nickname_tv.setText(MySharedPreferences.getInstance().getAlias());
            nickname_tv.setClickable(true);
            nickname_tv.setTextColor(Color.parseColor("#777777"));
//            nickname_tv.setCompoundDrawables(null,null,null,null);
        }

        if (TextUtil.isEmpty(userInfoResponse.getRealName())) {
            realName_tv.setText("请立即认证");
            realName_tv.setTextColor(Color.parseColor("#FD4A4A"));
//            realName_tv.setCompoundDrawables(null,null,null,null);
        } else {
            realName_tv.setText("已认证");
            realName_tv.setTextColor(Color.parseColor("#5C96F9"));
//            realName_tv.setCompoundDrawables(null,null,getResources().getDrawable(R.drawable.rigth_shijiao),null);
        }

        if (TextUtil.isEmpty(userInfoResponse.getPhone())) {
            phone_tv.setText("未绑定");
        } else {
            phone_tv.setText(PublicMethod.tv_yinCang(userInfoResponse.getPhone(), 3,
                    3));
        }

    }

    /**
     * 控件赋值
     */
    private void initBankInfo() {
        if (bankInfo == null) {
            bank_ll.setVisibility(View.GONE);
            bindBank_tv.setVisibility(View.VISIBLE);
        } else {
            bank_ll.setVisibility(View.VISIBLE);
            bindBank_tv.setVisibility(View.GONE);

            bankInfo_tv.setText("");
            SpannableString spanString = new SpannableString(bankInfo.getBankName());
            bankInfo_tv.append(spanString);
            spanString = TextStyleUtil.fontSizeSpan(new SpannableString("\n\n"+PublicMethod.tv_yinCang(bankInfo.getBankCard(), 3, 3)), 12);
            spanString.setSpan(new ForegroundColorSpan(Color.parseColor("#5C96F9")),0,spanString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            bankInfo_tv.append(spanString);

        }

        if (alipayInfo == null) {
            alipay_ll.setVisibility(View.GONE);
            bindAlipay_tv.setVisibility(View.VISIBLE);
        } else {
            alipay_ll.setVisibility(View.VISIBLE);
            bindAlipay_tv.setVisibility(View.GONE);

            alipayInfo_tv.setText("");
            SpannableString spanString = new SpannableString(alipayInfo.getBankName());
            alipayInfo_tv.append(spanString);
            spanString = TextStyleUtil.fontSizeSpan(new SpannableString("\n\n"+PublicMethod.tv_yinCang(alipayInfo.getBankCard(), 3, 3)), 12);
            spanString.setSpan(new ForegroundColorSpan(Color.parseColor("#5C96F9")),0,spanString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            alipayInfo_tv.append(spanString);
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        requestUserInfo();
        requestBankList();
    }

    private UserInfoResponse userInfoResponse;
    //查询用户信息
    private void requestUserInfo() {
        httpPostJson(CommandUtil.USERINFO, Commands.USERINFO, null, false);
    }

    //查询用户认证的银行卡列表
    private void requestBankList() {
        httpPostJson(CommandUtil.BANK_CARD_MANAGEMENT, Commands.FINDUSERDRAWBIND, null, false);
    }

    //请求解除银行卡绑定
    private void requestUnBind(String bankId) {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("id", bankId);
            httpPostJson(CommandUtil.BANK_DELETE, Commands.USERUNBIND, request_json, false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        switch (requestCode) {
            case CommandUtil.USERINFO:
                userInfoResponse = JSON.parseObject(resultBean.getData(), UserInfoResponse.class);
                kongJianFuZhi();
                break;
            case CommandUtil.BANK_CARD_MANAGEMENT:
                List<BankListResponse> bankListResponse = JSON.parseArray(resultBean.getData(), BankListResponse
                        .class);

                for (int i = 0; i < bankListResponse.size(); i++) {
                    if (bankListResponse.get(i).getDrawType() == 2) {
                        alipayInfo = bankListResponse.get(i);
                    } else {
                        bankInfo = bankListResponse.get(i);
                    }
                }
                initBankInfo();
                break;
            case CommandUtil.BANK_DELETE:
                AppToast.getToast().show("已删除");
                if (deleteType == 0) {
                    bankInfo = null;
                } else {
                    alipayInfo = null;
                }
                initBankInfo();
                break;
        }
    }

    @Override
    protected void onFailure(int requestCode, String errorCode, String message) {
        switch (requestCode) {
            case CommandUtil.BANK_CARD_MANAGEMENT:
                if(errorCode.equals("900003")){
                    bankInfo = null;
                    alipayInfo = null;
                    initBankInfo();
                }
                break;
        }
    }

    SureDialog sureDialog;
    /**
     * 确认支付对话框
     */
    public void showSureDialog() {

        SureDialog.OnDialogOKCancleClickListener listener = new SureDialog.OnDialogOKCancleClickListener() {

            @Override
            public void onOKClick() {

                if (deleteType == 0) {
                    requestUnBind(bankInfo.getId());
                } else {
                    requestUnBind(alipayInfo.getId());
                }
                sureDialog.dismiss();
            }

            @Override
            public void onCancleClick() {
                // TODO Auto-generated method stub
                sureDialog.dismiss();
            }
        };

        sureDialog = new SureDialog(this, listener, "", "确定要删除吗？", "确定", "取消");

        sureDialog.show();
    }
}
